<?php

namespace app\admin\controller;

use app\model\PolicyModel;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * Desc: 保险公司
 * Class Insurant
 * Created By Doyi
 * Date: 2018/11/26
 */

class Insurant extends Base
{
    protected $model;

    public function __construct(PolicyModel $policyModel)
    {
        parent::__construct();
        $this->model = $policyModel;
    }

    /**
     * 列表展示
     * @return \think\response\View
     */
    public function index()
    {
        $url = url("Insurant/insurantList");
        $this->assign('url',$url);
        return $this->fetch('insurant/index');
    }

    /**
     * 列表接口
     */
    public function insurantList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $arr = ['like'=>'u.username','1'=>'p.report_num'];
        $where = $this->select_param($arr,$param);
        if(isset($param['policy_status']) && !empty($param['policy_status']) ){
            $where['p.policy_status'] = $param['policy_status'];
        }else{
            $where['p.policy_status'] = 0;
        }
        $list =  $this->model->getinsurantListPage($page,$pageSize,$where,false);
        $count =  $this->model->getinsurantListPage(0,0,$where,true);

        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }



    public function info()
    {
        $param = $this->request->param();
            $info = Db::table('policy')
                ->where('policy.policy_num',$param['policy_num'])
                ->join('policy_item','policy.policy_num=policy_item.policy_num','left')
                ->field('policy_item.*,policy.insurance_type,policy.report_num,policy.policy_status,policy.p_id')
                ->find();
            if($info['policy_status'] == 0){
                $where = [
                    'p_id'=>$info['p_id'],
                    'status'=>1,
                ];
                $returnMsg = Db::table('return_policy')->where($where)->select();
                $data = [];
                foreach ($returnMsg as $k=>$v){
                    $str = '&nbsp;&nbsp;&nbsp;&nbsp;';
                    if($v['extend_table'] == 'user_info'){
                        $user =  Db::table('user_info')->where('ui_id',$v['extend_id'])->find();
                        $name = $user['username'].'(业务员)';
                    }else{
                        $user =  Db::table('admin')->where('id',$v['extend_id'])->find();
                        $name = $user['admin_name'].'(后台人员)';
                    }
                    if(!empty($v['policy_return'])){
                        $data['policy_return'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['policy_return'];
                    }
                    if(!empty($v['goods_return'])){
                        $data['goods_return'][] ='驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['goods_return'];
                    }
                    if(!empty($v['under_return'])){
                        $data['under_return'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['under_return'];
                    }
                    if(!empty($v['situation_return'])){
                        $data['situation_return'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['situation_return'];
                    }
                    if(!empty($v['info_return'])){
                        $data['info_return'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['info_return'];
                    }
                    if(!empty($v['certificate_return'])){
                        $data['certificate_return'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['certificate_return'];
                    }
                }
                $this->assign('data',$data);
            }

            $info['insurance_type'] = explode(',',$info['insurance_type']);
            $str = [];
            foreach($info['insurance_type'] as $k =>$v){
                $res = Db::table('insurance_type')->where('it_id',$v)->find();
                $str[] =$res['it_name'];
            }
            $info['insurant_type_name'] = $str;
            $info['present_img'] = json_decode($info['present_img']);
            $info['present_video'] = json_decode($info['present_video']);

            $info['goods_list'] = json_decode($info['goods_list']);
            $name = [];
            if( $info['goods_list']){
                foreach ($info['goods_list'] as $key=>$val){
                    $name[]= basename($info['goods_list']);
                }
            }
            $info['list_name'] = $name;
            $this->assign('info',$info);
            $this->assign('is_show',$param['is_show']);
        return $this->fetch('insurant/info');

    }

    public function save_return()
    {
        $param = $this->request->param();
        $this->assign('p_id',$param['p_id']);
        $this->assign('key',$param['key']);
        return $this->fetch('insurant/save_return');
    }

    public function save()
    {
        $param = $this->request->param();
        $key = $param['key'];
        $content = $param['content'];
        $p_id = $param['p_id'];
        $where = [
            'extend_id'=> $this->adminInfo['id'],
            'extend_table'=> 'admin',
            'status'=>1
        ];
        $data = [
            $key => $content,
            'p_id' =>$p_id,
            'created_at'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::table('return_policy')->where($where)->find();
        if($res){
            try{
                Db::startTrans();
                Db::table('return_policy')->where($where)->update($data);
                Db::table('policy')->where('p_id',$p_id)->update(['policy_status'=>0,'updated_at'=>date('Y-m-d H:i:s',time())]);
                Db::commit();
                return json(['code'=>200,'msg'=>'保存成功']);
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>400,'msg'=>'保存失败']);
            }
        }else{
            try{
                Db::startTrans();
                Db::table('return_policy')->insert(array_merge($data,$where));
                Db::table('policy')->where('p_id',$p_id)->update(['policy_status'=>0,'updated_at'=>date('Y-m-d H:i:s',time())]);
                Db::commit();
                return json(['code'=>200,'msg'=>'保存成功']);
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>400,'msg'=>'保存失败']);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function pass()
    {
        $param = $this->request->param();
        $res = Db::table('policy')->where('p_id',$param['p_id'])->update(['policy_status'=>5]);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功,信息已经转入被保险人资料未确认']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }
}